/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.DataRetrieval;

import Utilities.Constants;
import Utilities.Constants.VirtualResourceType;
import Utilities.Constants.ResourceIdType;
import Utilities.Constants.ResourceRequestType;
import Utilities.Constants.VirtualResourcesRetrievalLimit;
import Utilities.Functions;

/**
 * This is the superclass for all data retrieval requests for VirtualBox resources. <br><br>
 *
 * VirtualBox resources are: virtual machines, virtual media and networks. These
 * resources have UUID type IDs.
 *
 * @author Angel Sanadinov
 */
public abstract class VBoxResourceRequest extends DataRetrievalRequest
{
    private VirtualResourcesRetrievalLimit limit;   //the resource retrieval limit
    private String resourceId;                      //id of the requested resource
    private int ownerId;                            //owner id (for limiting multiple resources)

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     *
     * @see VirtualResourcesRetrievalLimit#INVALID_LIMIT
     * @see Constants#INVALID_USER_ID
     */
    public VBoxResourceRequest()
    {
        super();
        this.resourceId = null;
        this.limit = VirtualResourcesRetrievalLimit.INVALID_LIMIT;
        this.ownerId = Constants.INVALID_USER_ID;
    }

    /**
     * Constructs a request for a single resource with the specified resource id.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the resource resides
     * @param resourceId the id of the required resource
     */
    public VBoxResourceRequest(int requestorId, int serverId, String resourceId)
    {
        super(requestorId, serverId, ResourceRequestType.SINGLE_RESOURCE);
        this.resourceId = resourceId;
        this.limit = VirtualResourcesRetrievalLimit.INVALID_LIMIT;
        this.ownerId = Constants.INVALID_USER_ID;
    }

    /**
     * Constructs a request for multiple resources, limited by the specified
     * resource retrieval limit.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the resource resides
     */
    public VBoxResourceRequest(int requestorId, int serverId)
    {
        super(requestorId, serverId, ResourceRequestType.MULTIPLE_RESOURCES);
        this.resourceId = null;
        this.limit = VirtualResourcesRetrievalLimit.NO_LIMIT;
        this.ownerId = Constants.INVALID_USER_ID;
    }

    /**
     * Constructs a request for multiple resources owned by the user with the
     * specified id. <br><br>
     *
     * The limit is set to be <code>VirtualResourcesRetrievalLimit.OWNER_ID</code>.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the resource resides
     * @param ownerId the owner of the requested resources
     *
     * @see VirtualResourcesRetrievalLimit
     */
    public VBoxResourceRequest(int requestorId, int serverId, int ownerId)
    {
        super(requestorId, serverId, ResourceRequestType.MULTIPLE_RESOURCES);
        this.resourceId = null;
        this.limit = VirtualResourcesRetrievalLimit.OWNER_ID;
        this.ownerId = ownerId;
    }

    /**
     * Retrieves the resource id associated with this object.
     *
     * @return the resource id
     */
    public String getResourceId()
    {
        return resourceId;
    }

    /**
     * Retrieves the resource retrieval limit associated with this object.
     *
     * @return the resource retrieval limit, if available or <code>INVALID_LIMIT</code>
     *         otherwise
     *
     * @see VirtualResourcesRetrievalLimit#INVALID_LIMIT
     */
    public VirtualResourcesRetrievalLimit getLimit()
    {
        return limit;
    }

    /**
     * Retrieves the user id of the resources owner. <br><br>
     *
     * <b>Note:</b> <i>Only available when requesting multiple resources, limited
     * by owner id.</i>
     *
     * @return the owner id, if available or <code>INVALID_USER_ID</code> otherwise
     *
     * @see Constants#INVALID_USER_ID
     */
    public int getOwnerId()
    {
        return ownerId;
    }

    /**
     * Sets the resource id that will be associated with this object.
     *
     * @param resourceId the resource id (UUID)
     */
    public void setResourceId(String resourceId)
    {
        this.resourceId = resourceId;
    }

    /**
     * Sets the resources retrieval limit. <br><br>
     *
     * <b>Note:</b> <i>It is only used then requesting multiple resources.</i>
     *
     * @param limit the resources retrieval limit
     */
    public void setLimit(VirtualResourcesRetrievalLimit limit)
    {
        this.limit = limit;
    }

    /**
     * Sets the owner id of the resources that are to be requested, when limiting
     * the retrieval by owner id.
     *
     * @param ownerId the owner id
     */
    public void setOwnerId(int ownerId)
    {
        this.ownerId = ownerId;
    }

    /**
     * Retrieves the type of resource that is being requested.
     *
     * @return the resource type
     */
    public abstract VirtualResourceType getResourceType();

    
    @Override
    protected boolean isResourceIdValid()
    {
        if(Functions.isUUIDValid(resourceId))
            return true;
        else
            return false;
    }

    /**
     * Retrieves the type of the resource id. <br><br>
     *
     * <b>Note:</b> <i>For VirtualBox object, the resource id is always an UUID.</i>
     *
     * @return <code>ResourceIdType.UUID</code>
     */
    @Override
    public ResourceIdType getResourceIdType()
    {
       return ResourceIdType.UUID;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && (limit != VirtualResourcesRetrievalLimit.OWNER_ID || Functions.isUserIdValid(ownerId)))
            return true;
        else
            return false;
    }
}
